package com.tma.internship.ClassesInfo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;

import com.tma.internship.connectserver.ServerInfo;

import android.content.Context;
import android.util.Log;

public class ControlBookMark {
	private static final String fileName = "bookmark.xml";
	private Context context;
	public ControlBookMark(Context context){
		this.context = context;
	}
	private String getStringTime(){
        Format formatter;
        Date date = new Date();
        formatter = new SimpleDateFormat("dd/mm/yyyy hh:mm:ss a");
        return formatter.format(date);
	}
	public void write(String ebookName,String cmt,int page){
		try {
			FileInputStream fin = context.openFileInput(fileName);
			ObjectInputStream oin = new ObjectInputStream(fin);
			HashMap<String,BookMarkInfo> listInfo = (HashMap<String, BookMarkInfo>) oin.readObject();
			if (listInfo == null){
				listInfo = new HashMap<String, BookMarkInfo>();
			}
			BookMarkInfo thisBook = listInfo.get(ebookName);
			if (thisBook == null){
				ArrayList<CommentInfo> lsCmtInfo = new ArrayList<CommentInfo>();
				lsCmtInfo.add(new CommentInfo(cmt, page, getStringTime(),ebookName));
				thisBook = new BookMarkInfo(ebookName, lsCmtInfo);
				listInfo.put(ebookName, thisBook);
			} else {
				thisBook.getListCmt().add(new CommentInfo(cmt, page, getStringTime(),ebookName));
			}
			FileOutputStream fout = context.openFileOutput(fileName, context.MODE_PRIVATE);
			ObjectOutputStream oout = new ObjectOutputStream(fout);
			oout.writeObject(listInfo);
			oin.close();
			oout.close();
		} catch (Exception e){
			try {
				ArrayList<CommentInfo> lsCmtInfo = new ArrayList<CommentInfo>();
				lsCmtInfo.add(new CommentInfo(cmt, page, getStringTime(),ebookName));
				BookMarkInfo thisBook = new BookMarkInfo(ebookName, lsCmtInfo);
				HashMap<String,BookMarkInfo> listInfo = new HashMap<String, BookMarkInfo>();
				listInfo.put(ebookName, thisBook);
				FileOutputStream fout = context.openFileOutput(fileName, context.MODE_PRIVATE);
				ObjectOutputStream oout = new ObjectOutputStream(fout);
				oout.writeObject(listInfo);
				oout.close();
			} catch (Exception ex){
				ex.printStackTrace();
			}
			return;
		}
	}
	public void deleteCmtOnBook(String book){
		try{
			FileInputStream fin = context.openFileInput(fileName);
			ObjectInputStream oin = new ObjectInputStream(fin);
			HashMap<String,BookMarkInfo> listInfo = (HashMap<String, BookMarkInfo>) oin.readObject();
			oin.close();
			if (listInfo == null) return;
			listInfo.remove(book);
			FileOutputStream fout = context.openFileOutput(fileName, context.MODE_PRIVATE);
			ObjectOutputStream oout = new ObjectOutputStream(fout);
			oout.writeObject(listInfo);
			oout.close();
		} catch (Exception e){
			
		}
	}
	public void deleteCmtOnPage(String book,int page){
		try{
			FileInputStream fin = context.openFileInput(fileName);
			ObjectInputStream oin = new ObjectInputStream(fin);
			HashMap<String,BookMarkInfo> listInfo = (HashMap<String, BookMarkInfo>) oin.readObject();
			oin.close();
			if (listInfo == null) return;
			BookMarkInfo bookInfo = listInfo.get(book);
			if (bookInfo == null) return;
			CommentInfo cmt = null;
			for (CommentInfo tmp : bookInfo.getListCmt()){
				if (tmp.getPage() == page){
					cmt = tmp;
					break;
				}
			}
			bookInfo.getListCmt().remove(cmt);
			FileOutputStream fout = context.openFileOutput(fileName, context.MODE_PRIVATE);
			ObjectOutputStream oout = new ObjectOutputStream(fout);
			oout.writeObject(listInfo);
			oout.close();
		} catch (Exception e){
			return;
		}
	}
	public void deleteAll(){
		try {
			FileOutputStream fout = context.openFileOutput(fileName, context.MODE_PRIVATE);
			ObjectOutputStream oin = new ObjectOutputStream(fout);
			oin.writeObject(new HashMap<String, BookMarkInfo>());
			oin.close();
		} catch (Exception e){
			
		}
	}
	public List<CommentInfo> listCmtInBook(String ebookName){
		try {
			FileInputStream fin = context.openFileInput(fileName);
			ObjectInputStream oin = new ObjectInputStream(fin);
			HashMap<String,BookMarkInfo> listInfo = (HashMap<String, BookMarkInfo>) oin.readObject();
			BookMarkInfo bookInfo = listInfo.get(ebookName);
			oin.close();
			if (bookInfo == null) {
				Log.d("time", "null");
				return null;
			}
			List<CommentInfo> listCmtInfo = new ArrayList<CommentInfo>();
			for (CommentInfo tmp:bookInfo.getListCmt()){
				listCmtInfo.add(tmp);
				Log.d("time", tmp.getTime());
			}
			
			return listCmtInfo;
		} catch (Exception e){
			e.printStackTrace();
			return null;
		}
	}
	public List<CommentInfo> listCmtAll(){
		try {
			FileInputStream fin = context.openFileInput(fileName);
			ObjectInputStream oin = new ObjectInputStream(fin);
			HashMap<String,BookMarkInfo> listInfo = (HashMap<String, BookMarkInfo>) oin.readObject();
			oin.close();
			if (listInfo == null)  return null;
			else {
				List<CommentInfo> listCmtInfo = new ArrayList<CommentInfo>();
				Iterator<Entry<String, BookMarkInfo>> iter = listInfo.entrySet().iterator();
				while (iter.hasNext()) {
                    LinkedHashMap.Entry<String, BookMarkInfo> entry = (LinkedHashMap.Entry) iter.next();
                    BookMarkInfo bookInfos = entry.getValue();
                    for (CommentInfo tmp:bookInfos.getListCmt()){
                    	listCmtInfo.add(tmp);
                    }
				}
				return listCmtInfo;
			}
		} catch (Exception e){
			return null;
		}
	}
	
}
